package cate.game.activity.convertgod;

import cate.common.table.item.equip.row.EquipBaseRow;
import cate.common.util.GameResult;
import cate.game.activity.base.core.RoleActivitySingle;
import cate.game.activity.convertgod.msg.ConvertGodInfoResp;
import cate.game.client.msg.MergeMsgSender;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.res.RewardShow;
import cate.game.role.bag.equip.Equip;

public class ConvertGodActivity extends RoleActivitySingle {
    public GameResult<ConvertGodActivity> convert(String from, int to) {
        GameResult<ConvertGodActivity> r = new GameResult<>();
        r.data = this;
        Equip equip = role.getBag().getEquip().getItem(from);
        if (equip == null) {
            return r.fail("用于置换的神装不存在");
        }
        if (equip.tid == to) {
            return r.fail("无法置换相同的神装");
        }
        EquipBaseRow equipBaseRow = role.getGame().table.equip.base.get(to);
        if (equipBaseRow == null) {
            return r.fail("目标灵器不存在");
        }
        if (equip.getTpl(role.getGame()).star != equipBaseRow.star) {
            return r.fail("只能兑换星级相同的神装");
        }
        //消耗物品
        MixRes cost = new MixRes(equipBaseRow.convertCost);
        GameResult<MixResItem> consume = cost.consume(role);
        if (!consume.ok()) {
            return r.fail(consume);
        }
        MergeMsgSender sender = new MergeMsgSender();
        RewardShow show = equip.gemsOn.offAll(role, null, sender, false);
        role.getBag().getEquip().removeItemByUid(equip.uid, 1, sender);
        Equip add = new Equip(role, to, 1);
        add.psionicExp = equip.psionicExp;
        add.campMark = equip.campMark;
        role.getBag().getEquip().addItem(add, sender);
        sender.send(role,true);
        MixRes rew = new MixRes(add);
        rew.addList(show.tplRwd);
        role.getGame().notice.dialogReward(role, rew);
        return r;
    }

    @Override
    public void initSend() {
        super.initSend();
        noticeUpdate();
    }

    public void noticeUpdate() {
        role.sendNow(new ConvertGodInfoResp(this));
    }
}
